<template>
  <div class="app-container">
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item>
        <el-button type="primary" @click="setLoad">拉取活动设置</el-button> 
        
        <el-button type="primary" @click="setRefresh">刷新题库</el-button>
      </el-form-item>
    </el-form>
     <el-dialog title="参数设置" :visible.sync="dialogFormVisible" width="40%">
      <el-form ref="form" :model="form" label-position="left" label-width="100px">
            <el-form-item label="参数生成" :label-width="formLabelWidth">
              <el-input v-model="form.json" autocomplete="off"></el-input>
              <el-button type="primary" size="mini" @click="generate">生成</el-button>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" size="mini" @click="onSubmits">确定</el-button>
              <el-button size="mini" @click="Cancel">取消</el-button>
            </el-form-item>
      </el-form>
     </el-dialog>
      <el-table
       :data="tableData.slice((currentPage-1)*pagesize,currentPage*pagesize)"
       style="width: 100%">
       <el-table-column
         label="设置"
         width="500">
         <template slot-scope="scope">
           <span>{{ scope.row }}</span>
         </template>
       </el-table-column>
      <el-table-column label="操作"> 
          <el-button
             size="mini"
             type="primary"
             @click="setting">设置参数</el-button>                
       </el-table-column>
        </el-table>
        <div class="pages" v-if="tableData!=''">
       <div class="block">
         <el-pagination
           @current-change="handleCurrentChange"
           :current-page.sync="currentPage"
           :page-size="pagesize"
           layout="prev, pager, next, jumper"
           :total="total">
         </el-pagination>
       </div>
      </div>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import Method from '@/utils/proto'
import {posts} from '@/api/api'

export default {
    data() {
      return {
        tableData:[],
         currentPage: 1,
        total:100,
        pagesize:15,
        jsonbloon:false,
        formInline: {
           code: '',
        },
        form:{
          json:''
        },
        dialogFormVisible:false,
        formLabelWidth:'100px'
      }
    },
    methods: {
        handleCurrentChange(val){
      this.currentPage = val;
      },
    
      onSubmits(){
        let roles=this.$store.getters.roles
        if(roles==10000000||roles==11000000||roles=='00000000'){
         this.$message({
           type:'warning',
           message:'没有权限执行此操作'
         })
         return
       }
        const that = this
        let data={
        method:Method.Method.MSG_AGENT_SAVE_ACT_SETTING,
        token:this.$store.getters.token,
        setting:that.form.json
      }
       posts(data).then((res) => {
         if(res.result==0){
           this.dialogFormVisible=false,
           this.setLoad()
             this.$message({
              type: "success",
              message: "设置成功!",
            });
         }
      })
      },
      setting(){
         this.dialogFormVisible=true
      },
      setLoad(){
      let data={
        method:Method.Method.MSG_AGENT_LOAD_ACT_SETTING,
        token:this.$store.getters.token,
        code:this.formInline.code,
      }
       posts(data).then((res) => {       
         let newset = res.setting    
         let set = []
         let sset = []
         set.push(newset)
         let nset = set[0].replace("\"","").replace("\"","")
         sset.push(nset)
        this.tableData = sset
        this.total = sset.length
      }).catch()
      },
      onSubmitLoad(){
        let data = {
          method:Method.Method.MSG_AGENT_LOAD_ACT_SETTING,
          token:this.$store.getters.token,
          code:this.lcode.code
        }
        posts(data).then((res) => {
      }).catch()
      },
      setRefresh(){
      const loading = this.$loading({
          lock: true,
          text: 'Loading',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
      });
      this.dialogFormVisiblerefresh=true
      let data={
        method:Method.Method.MSG_AGENT_REFRESH_QUESTION,
        token:this.$store.getters.token,
      }
       posts(data).then((res) => {
         if(res.result==0){      
         setTimeout(() => {
          loading.close();
          this.$message({
              type: "success",
              message: "刷新成功",
            });
          }, 500);            
         }
          
      }).catch()
      },
      onSubmitRefresh(){
         let data = {
          method:Method.Method.MSG_AGENT_REFRESH_QUESTION,
          token:this.$store.getters.token,
          code:this.lcode.code
        }
        posts(data).then((res) => {
      }).catch()
      },
      Cancel(){
        this.dialogFormVisible=false
      },
      
      generate(){
        let that = this
        let json = this.form.json
        if(this.jsonbloon){
          return
        }
        that.form.json=`{library":${json}}`
        this.jsonbloon=true
      }

    },
    created:function(){
      
     },
  }
</script>

<style lang="scss" scoped>
 .pages{
    width: 100%;
    margin-top: 2%;
    display: flex;
    justify-content: center;
  }
</style>
